package service

import (
	"github.com/gin-gonic/gin"
	"goCmdb/internal/modules"
	"net/http"
)

func ServiceAdd(c *gin.Context)  {
	server := modules.ServiceInventory{}
	err := c.BindJSON(&server)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{})
		return
	}
	if server.ServiceAdd() {
		c.JSON(http.StatusOK, gin.H{
			"user":    server.User,
			"address": server.Address,
			"message": "hosts 添加成功",
			"status":  "successfully",
		})
	} else {
		c.JSON(http.StatusInternalServerError, gin.H{"message": "hosts 已存在或者添加过程出现错误"})
	}
}

func ServiceDelete(c *gin.Context)  {
	server := modules.ServiceInventory{}
	err := c.BindJSON(&server)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{})
		return
	}
	if server.ServiceDelete() {
		c.JSON(http.StatusOK, gin.H{
			"user":    server.User,
			"address": server.Address,
			"message": "hosts 删除成功",
			"status":  "successfully",
		})
	} else {
		c.JSON(http.StatusInternalServerError, gin.H{"message": "hosts 删除失败"})
	}
}

func ServiceUpdate(c *gin.Context)  {
	server := modules.ServiceInventory{}
	_ = c.BindJSON(&server)
	if server.ServiceUpdate(){
		c.JSON(http.StatusOK, gin.H{"message": "更新成功"})
	}else {
		c.JSON(http.StatusInternalServerError, gin.H{"message": "更新失败"})
	}
}

func ServiceGet(c *gin.Context)  {
	server := modules.ServiceInventory{}
	data,err := server.ServiceGet()
	if err != nil{
		c.JSON(http.StatusInternalServerError, gin.H{"data":"nil","message": err})
	}else {
		c.JSON(http.StatusOK, gin.H{"data":&data,"message": "successfully"})
	}
}